package fr.univ.lille1.server.webservice;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import fr.univ.lille1.server.domain.SessionKey;
import fr.univ.lille1.server.domain.User;
import fr.univ.lille1.server.domain.User.Type;
import fr.univ.lille1.server.manager.UserManager;

@Path("/user")
@Stateless
@LocalBean
@Produces("application/json")
public class UserWebService {

	@EJB
	private UserManager userManager;

	@PUT
	public SessionKey createUser(@FormParam("name") String name,
			@FormParam("password") String password,
			@FormParam("mail") String mail, @FormParam("type") String userType) {
		
		Type type = null;

		if(userType==null || userType.equals("STANDARD")){
			type = Type.STANDARD;
		}else{
			if(userType.equals("GOOGLE")){
				type = Type.GOOGLE;
			}
		}
		
		return userManager.createUser(name, password, mail,type);
	}

	@POST
	public User updateUser(@PathParam("id") int id,
			@FormParam("name") String name,
			@FormParam("password") String password,
			@FormParam("mail") String mail) {
		return userManager.updateUser(id, name, password, mail);
	}

	@GET
	@Path("{idOrMail}")
	public User findUserByIdOrMail(@PathParam("idOrMail") String idOrMail) {
		int id;
		try { 
	        id = Integer.parseInt(idOrMail); 
	    } catch(NumberFormatException e) { 
	        return userManager.findUserByMail(idOrMail);
	    }
		return userManager.findUserById(id);
	}

}
